using System;
using System.Runtime.Intrinsics;
using CUE4Parse.UE4.VirtualFileSystem;
using static System.Runtime.Intrinsics.X86.Aes;
using static System.Runtime.Intrinsics.Vector128;

namespace CUE4Parse.GameTypes.FSR.Encryption.Aes;

/// <summary>
/// Reversed by Spiritovod
/// </summary>
public static class FreeStyleReboundAes
{
    // 0x6D89D6DB4A5CC951E446BE95AAB054108057B22DF936A8E2F27F9FC00D16A103
    private static readonly Vector128<byte>[] RoundKeys =
    [
        Create(0xB0, 0x97, 0x9A, 0xC8, 0x52, 0x0B, 0x51, 0x95, 0x52, 0x54, 0x8F, 0x2D, 0x2C, 0x03, 0xE5, 0xED),
        Create(0xC4, 0x43, 0x7E, 0x4E, 0xE5, 0xE1, 0x58, 0x96, 0x46, 0xA9, 0x02, 0xD7, 0xF0, 0xA4, 0x09, 0xDC),
        Create(0x3D, 0x67, 0x0B, 0x6B, 0x53, 0xF2, 0xB1, 0xF8, 0x01, 0x1D, 0xAD, 0x88, 0x8B, 0xB0, 0x82, 0x3A),
        Create(0x05, 0x93, 0x6E, 0x5F, 0x21, 0xA2, 0x26, 0xD8, 0xA3, 0x48, 0x5A, 0x41, 0xB6, 0x0D, 0x0B, 0x0B),
        Create(0x65, 0x9A, 0x18, 0x00, 0x6E, 0x95, 0xBA, 0x93, 0x52, 0xEF, 0x1C, 0x70, 0x8A, 0xAD, 0x2F, 0xB2),
        Create(0x65, 0x9A, 0x18, 0x00, 0x6E, 0x95, 0xBA, 0x93, 0x52, 0xEF, 0x1C, 0x70, 0x8A, 0xAD, 0x2F, 0xB2),
        Create(0x05, 0x93, 0x6E, 0x5F, 0x8F, 0xD6, 0x09, 0xDF, 0xA6, 0xDB, 0x34, 0x1E, 0x97, 0xAF, 0x2D, 0xD3),
        Create(0x59, 0x1B, 0x90, 0x58, 0x0B, 0x0F, 0xA2, 0x93, 0x3C, 0x7A, 0xA6, 0xE3, 0xD8, 0x42, 0x33, 0xC2),
        Create(0x59, 0x1B, 0x90, 0x58, 0x0B, 0x0F, 0xA2, 0x93, 0x3C, 0x7A, 0xA6, 0xE3, 0xD8, 0x42, 0x33, 0xC2),
        Create(0x05, 0x93, 0x6E, 0x5F, 0xE7, 0x3E, 0xCC, 0x37, 0xA3, 0x48, 0x5A, 0x41, 0x18, 0x79, 0x24, 0x0C),
        Create(0x9C, 0xDF, 0x78, 0x0A, 0x52, 0x14, 0x32, 0xCB, 0x37, 0x75, 0x04, 0x70, 0xE4, 0x38, 0x95, 0x21),
        Create(0xC3, 0x4B, 0x61, 0xA1, 0xE2, 0xAD, 0xA2, 0x68, 0x44, 0x76, 0x96, 0x76, 0xBB, 0x31, 0x7E, 0x4D),
        Create(0x6D, 0x89, 0xD6, 0xDB, 0x4A, 0x5C, 0xC9, 0x51, 0xE4, 0x46, 0xBE, 0x95, 0xAA, 0xB0, 0x54, 0x10)
    ];

    private static void DecryptWithRoundKeys(byte[] input, int index, Vector128<byte>[] roundkeys)
    {
        var state = Create(input, index);
        var rounds = roundkeys.Length - 1;
        state = Xor(state, roundkeys[0]);
        for (var i = 1; i < rounds; i++)
        {
            state = Decrypt(state, roundkeys[i]);
        }

        state = DecryptLast(state, roundkeys[rounds]);
        state.CopyTo(input, index);
    }

    public static byte[] FSRDecrypt(byte[] bytes, int beginOffset, int count, bool isIndex, IAesVfsReader reader)
    {
        if (bytes.Length < beginOffset + count)
            throw new IndexOutOfRangeException("beginOffset + count is larger than the length of bytes");
        if (count % 16 != 0)
            throw new ArgumentException("count must be a multiple of 16");
        if (reader.AesKey == null)
            throw new NullReferenceException("reader.AesKey");

        var output = new byte[count];
        Array.Copy(bytes, beginOffset, output, 0, count);

        for (var i = 0; i < count / 16; i++)
        {
            DecryptWithRoundKeys(output, i * 16, RoundKeys);
        }

        return output;
    }
}